﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Common;

namespace Backend.JYD.EFDAL.DAL
{
	public class EmpInfoDAL : Backend.JYD.IDAL.IEmpInfo
	{
		#region 查询数据EmpInfo

        public List<EmpInfo> GetAllEmpInfo()
        {
            try
            {
                using (JydDataContext db = new JydDataContext())
                {
                    return db.EmpInfoes.ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
                return null;
            }
        }

        public List<EmpInfo> GetEmpInfoByEmpIds(List<Int64> empIds, bool isAll)
        {
            Int64[] ids = empIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.EmpInfoes.Where(p => ids.Contains(p.EmpId));
                if (!isAll) query = query.Where(p => p.Status > 0);
                return query.ToList();
            }
        }

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<EmpInfo> GetEmpInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<EmpInfo> query = db.EmpInfoes;
				//其他条件代码
				return new PageInfo<EmpInfo>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public EmpInfo GetEmpInfoByEmpId(Int64 empId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<EmpInfo> query = db.EmpInfoes.Include("EmpAuthInfoes").Include("EmpRoleInfoes").Where(p => p.EmpId == empId);
				return query.FirstOrDefault();
			}
		}

        public EmpInfo GetLoginEmpInfo(string loginName, string pwd)
        { 
            using(JydDataContext db = new JydDataContext())
            {
                var query = db.EmpInfoes.Where(p => p.LoginName == loginName && p.PassWord == pwd);
                return query.FirstOrDefault();
            }
        }
		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据EmpInfo
		/// </summary>
		public Int64 AddEmpInfo(EmpInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.EmpInfoes.Add(info);
				db.SaveChanges();
				return info.EmpId;
			}
		}
		#endregion

		#region 修改数据EmpInfo

		/// <summary>
		/// 根据字段（EmpId）修改数据EmpInfo
		/// </summary>
		public bool UpdateEmpInfo(EmpInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				var query = db.EmpInfoes.Where(p => p.EmpId == info.EmpId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
						//.......
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion

	}
}